const express = require("express");
const { Pool } = require("pg");
const app = express();
//设置启动选项
app.listen(1570, () => {
  console.log("服务器启动成功");
});
//配置数据库库
const pool = new Pool({
  user: "stellar",
  host: "localhost",
  password: "gMcpAIdH7Ce6znwNZUjr",
  database: "core",
  port: "5432",
});

//通过/ 路径查询表中的数据然后渲染出来
app.get("/", (req, res) => {
res.send("后台服务器架设成功")
  
});

//查看数据库剩余连接数
app.get("/count", (req, res) => {
  // console.log(req.url)
  pool.query(
    "select max_conn-now_conn as resi_conn from (select setting::int8 as max_conn,(select count(*) from pg_stat_activity) as now_conn from pg_settings where name = 'max_connections') t;",
    (err, resp) => {
      try {
        if (err) {
          res.send("数据库连接失败");
          pool.end;
        }

        res.send(resp.rows);
        pool.end;
      } catch (er) {
        console.log(er);
      }
    }
  );
});
//关闭空闲连接

app.get("/close", (req, res) => {
  // console.log(req.url)
  pool.query(
    "select datname,pid,application_name,state from pg_stat_activity; ",
    (err, resp) => {
      try {
        if (err) {
          res.send("数据库连接失败");
          pool.end;
        }

        res.send(resp.rows);
        pool.end;
      } catch (er) {
        console.log(er);
      }
    }
  );
});
//当前总共正在使用的连接数

app.get("/tvz", (req, res) => {
  // console.log(req.url)
  pool.query("select count(1) from pg_stat_activity;", (err, resp) => {
    try {
      if (err) {
        res.send("数据库连接失败");
        pool.end;
      }

      res.send(resp.rows);
      pool.end;
    } catch (er) {
      console.log(er);
    }
  });
});

//显示系统允许的最大连接数

app.get("/max", (req, res) => {
  // console.log(req.url)
  pool.query("show max_connections;", (err, resp) => {
    try {
      if (err) {
        res.send("数据库连接失败");
        pool.end;
      }

      res.send(resp.rows);
      pool.end;
    } catch (er) {
      console.log(er);
    }
  });
});


//accountdata
app.get("/acc", (req, res) => {
  // console.log(req.url)
  pool.query("SELECT * FROM accountdata limit 100", (err, resp) => {
    try {
      if (err) {
        res.send("数据库连接失败");
        pool.end;
      }

      res.send(resp.rows);
      pool.end;
    } catch (er) {
      console.log(er);
    }
  });
});
// claimablebalance
app.get("/cl", (req, res) => {
  // console.log(req.url)
  pool.query("SELECT * FROM claimablebalance limit 100", (err, resp) => {
    try {
      if (err) {
        res.send("数据库连接失败");
        pool.end;
      }

      res.send(resp.rows);
      pool.end;
    } catch (er) {
      console.log(er);
    }
  });
});
// ledgerheaders
app.get("/led", (req, res) => {
  // console.log(req.url)
  pool.query("SELECT * FROM ledgerheaders limit 100", (err, resp) => {
    try {
      if (err) {
        res.send("数据库连接失败");
        pool.end;
      }

      res.send(resp.rows);
      pool.end;
    } catch (er) {
      console.log(er);
    }
  });
});
// peers
app.get("/peers", (req, res) => {
  // console.log(req.url)
  pool.query("SELECT * FROM peers limit 100", (err, resp) => {
    try {
      if (err) {
        res.send("数据库连接失败");
        pool.end;
      }

      res.send(resp.rows);
      pool.end;
    } catch (er) {
      console.log(er);
    }
  });
});
// pubsub
app.get("/pub", (req, res) => {
  // console.log(req.url)
  pool.query("SELECT * FROM pubsub limit 100", (err, resp) => {
    try {
      if (err) {
        res.send("数据库连接失败");
        pool.end;
      }

      res.send(resp.rows);
      pool.end;
    } catch (er) {
      console.log(er);
    }
  });
});
// quoruminfo
app.get("/qu", (req, res) => {
  // console.log(req.url)
  pool.query("SELECT * FROM quoruminfo limit 100", (err, resp) => {
    try {
      if (err) {
        res.send("数据库连接失败");
        pool.end;
      }

      res.send(resp.rows);
      pool.end;
    } catch (er) {
      console.log(er);
    }
  });
});
// scphistory
app.get("/scp", (req, res) => {
  // console.log(req.url)
  pool.query("SELECT * FROM scphistory  limit 100", (err, resp) => {
    try {
      if (err) {
        res.send("数据库连接失败");
        pool.end;
      }

      res.send(resp.rows);
      pool.end;
    } catch (er) {
      console.log(er);
    }
  });
});
// scpquorums
app.get("/scpq", (req, res) => {
  // console.log(req.url)
  pool.query("SELECT * FROM  scpquorums limit 100", (err, resp) => {
    try {
      if (err) {
        res.send("数据库连接失败");
        pool.end;
      }

      res.send(resp.rows);
      pool.end;
    } catch (er) {
      console.log(er);
    }
  });
});
// storestate
app.get("/st", (req, res) => {
  // console.log(req.url)
  pool.query("SELECT * FROM storestate  limit 100", (err, resp) => {
    try {
      if (err) {
        res.send("数据库连接失败");
        pool.end;
      }

      res.send(resp.rows);
      pool.end;
    } catch (er) {
      console.log(er);
    }
  });
});
// trustlines
app.get("/tr", (req, res) => {
  // console.log(req.url)
  pool.query("SELECT * FROM trustlines limit 100", (err, resp) => {
    try {
      if (err) {
        res.send("数据库连接失败");
        pool.end;
      }

      res.send(resp.rows);
      pool.end;
    } catch (er) {
      console.log(er);
    }
  });
});
// txfeehistory
app.get("/tx", (req, res) => {
  // console.log(req.url)
  pool.query("SELECT * FROM txfeehistory limit 100", (err, resp) => {
    try {
      if (err) {
        res.send("数据库连接失败");
        pool.end;
      }

      res.send(resp.rows);
      pool.end;
    } catch (er) {
      console.log(er);
    }
  });
});
// txhistory
app.get("/txh", (req, res) => {
  // console.log(req.url)
  pool.query("SELECT * FROM txhistory limit 100", (err, resp) => {
    try {
      if (err) {
        res.send("数据库连接失败");
        pool.end;
      }

      res.send(resp.rows);
      pool.end;
    } catch (er) {
      console.log(er);
    }
  });
});

// offer
app.get("/offers", (req, res) => {
  // console.log(req.url)
  pool.query("SELECT * FROM offers  limit 100", (err, resp) => {
    try {
      if (err) {
        res.send("数据库连接失败");
        pool.end;
      }

      res.send(resp.rows);
      pool.end;
    } catch (er) {
      console.log(er);
    }
  });
});
